package com.wave.wavesystem.serve.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.common.util.Func;
import com.wave.wavesystem.serve.mapper.UserMapper;
import com.wave.wavesystem.serve.pojo.entity.User;
import com.wave.wavesystem.serve.pojo.query.CenterSearchQuery;
import com.wave.wavesystem.serve.pojo.query.UserQuery;
import com.wave.wavesystem.serve.service.IUserService;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public QueryChain<User> getQueryChain(UserQuery query) {
        QueryChain<User> queryChain = queryChain();
        if (Func.isNotEmpty(query.getUsername())) {
            queryChain.like(User::getUsername, query.getUsername());
        }
        if (Func.isNotEmpty(query.getAccount())) {
            queryChain.eq(User::getAccount, query.getAccount())
                    .or(User::getAccount).like(query.getAccount());
        }
        return queryChain;
    }

    @Override
    public Integer getUserLevelRank(Long userId) {
        return mapper.getUserLevelRank(userId);
    }

    @Override
    public Page<User> search(CenterSearchQuery searchQuery) {
        String keyword = searchQuery.getKeyword();
        QueryChain<User> queryChain = this.queryChain()
                .like(User::getUsername, keyword)
                .or(User::getAccount)
                .like(keyword)
                .or(User::getDesc)
                .like(keyword);
        return this.page(new Page<>(searchQuery.getPageNum(), searchQuery.getPageSize()), queryChain);
    }
}
